package com.lsk.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayUserCertifyOpenCertifyRequest;
import com.alipay.api.request.AlipayUserCertifyOpenInitializeRequest;
import com.alipay.api.response.AlipayUserCertifyOpenCertifyResponse;
import com.alipay.api.response.AlipayUserCertifyOpenInitializeResponse;

import java.net.URLEncoder;

/**
 * 支付宝身份认证
 */
public class AliyunBcprovUtil {

    //appId
    static String APP_ID="2021001160630522";
    //应用私匙
    static String APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCkrke0ovJIFbLj0rySR4zvBy58EUdIqPY73C9yRZ7sZf57z5+u3Ht6J+dsHcV2oqYg79VYmMrXyuFVHdk3OXydaVt4DUj3WopT6MNTj5s7ulbo9uqnt7r6MZT/fexj0pGvmrw5VCFMvJe5o75N66fZrMKj15hotyoMkPQ6PvKQdG4mKd0g7ZfWnEe/vaJYCTnw+XQ+Z3jES8SddarbjCNeJI/XJj880wiJBKdMkGifqU2YQmQ8k+u6dO/sMtIs+Bp2Sy0ABgcH4ITldgR3C2T5Zg1bFa0KBiritZG3IwwTbu2thnhqUUDQ2dBD17dd8Fe7M6BCkxEfH7EfkjLBsUrnAgMBAAECggEAF+oLFas3/cnQ4Y6fvapQSAHF+VRVArMulsDBzxlXG8Mv8nDTIoR4FeQnkia8nx69ezj4xVXi1vVqRX8BJSje9Mjw9H0RZIIsZkT94GuRQlor9sasitB+gYijt3pk0pYoe9S/kp+p+kfEzLt/jjczd8PDvCE5y7MVkx+Vp8OuW9Wf1+oSSKU8HULF2zVv4egSBP6vh/fEPiWcGOQLyWM1hpipV8U4koGMIG/CFGYiUM6QqJYrrqG+jZTQuSab4F95nY6RQdSkWK0Xq9ty7n08hWJ58CltNbkUX7GPjx8J18W9y9+1Op6yEx5KNY5ukBCmas9C0QmB7yrLqDGqTj/SOQK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DLuEdyr8/5nfQov3LDrY1BZbrMF7DMqbs8S7k6zml/RD30JtwAriBXX/vPa07LLQGKxRgEjmQpPIxHkZy8tEgtaP5MmCJ4wWVLSx2T1dQlRy11v6X9ohRcRL5OTq9gGE7YQK2BeIuy/vhlF51x+OTOLC70ZgaAte6ryVAe8amQN5Mco0aME59oA9rE4lE6X3Aim5y3s/SGbu5+GJXOv62S0oCbsQ/FodxSACgevIcVCOeXQIDAQAB";

    //1. 初始化身份认证单据号
    public static String init(String name,String cardNumber,String token) throws AlipayApiException {

        String certifyId="";

        AlipayClient alipayClient   =   new DefaultAlipayClient( "https://openapi.alipay.com/gateway.do" ,  APP_ID ,
                APP_PRIVATE_KEY ,  "json" ,  "utf-8" ,  ALIPAY_PUBLIC_KEY ,  "RSA2" );
        AlipayUserCertifyOpenInitializeRequest request   =   new   AlipayUserCertifyOpenInitializeRequest ();

        //构造身份信息json对象
        JSONObject   identityObj   =   new JSONObject();
        //身份类型，必填，详细取值范围请参考接口文档说明
        identityObj . put ( "identity_type" ,  "CERT_INFO" );
        //证件类型，必填，详细取值范围请参考接口文档说明
        identityObj . put ( "cert_type" ,  "IDENTITY_CARD" );
        //真实姓名，必填
        identityObj . put ( "cert_name" ,  name);
        //证件号码，必填
        identityObj . put ( "cert_no" ,  cardNumber);

        //构造商户配置json对象
        JSONObject   merchantConfigObj   =   new   JSONObject ();
        // 设置回调地址,必填. 如果需要直接在支付宝APP里面打开回调地址使用alipay协议，参考下面的案例：appId用固定值 20000067，url替换为urlEncode后的业务回跳地址
        // alipays://platformapi/startapp?appId=20000067&url=https%3A%2F%2Fapp.cqkqinfo.com%2Fcertify%2FzmxyBackNew.do
        String reictUtl="http://apihzijy.19xw.net/mobile/zfbRealNameCallBack?token="+token+"&";

        merchantConfigObj . put ( "return_url" ,  "alipays://platformapi/startapp?appId=20000067&url="+reictUtl );

        //构造身份认证初始化服务业务参数数据

        JSONObject   bizContentObj   =   new   JSONObject ();
        //商户请求的唯一标识，推荐为uuid，必填
        bizContentObj . put ( "outer_order_no" ,  UuidUtil.get32UUID() );
        bizContentObj . put ( "biz_code" ,  "FACE" );
        bizContentObj . put ( "identity_param" ,  identityObj );
        bizContentObj . put ( "merchant_config" ,  merchantConfigObj );
        request . setBizContent ( bizContentObj . toString ());

        //发起请求
        AlipayUserCertifyOpenInitializeResponse response   =   alipayClient . execute ( request );
        if  ( response . isSuccess ()) {
            System . out . println ( "调用成功" );
            //接口调用成功，从返回对象中获取certify_id
            certifyId   =   response . getCertifyId ();
            //执行后续流程...
        }  else  {
            System . out . println ( "调用失败" );
        }


        return certifyId;

    }

    /**
     * 认证开始
     */
    public static ReturnData cprovUtil(String name,String cardNumber,String token) throws AlipayApiException {

        ReturnData returnData=new ReturnData();

        String certify_id=init(name,cardNumber,token);

        AlipayClient alipayClient =
                new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                        APP_ID,APP_PRIVATE_KEY,
                        "json","GBK",ALIPAY_PUBLIC_KEY,"RSA2");
        AlipayUserCertifyOpenCertifyRequest request = new AlipayUserCertifyOpenCertifyRequest();
        request.setBizContent("{" +
                "\"certify_id\":\""+certify_id+"\"" +
                "  }");
        AlipayUserCertifyOpenCertifyResponse response = alipayClient.pageExecute(request);



        if(response.isSuccess()){
            System.out.println("调用成功");

            returnData.setResult(true);


            //String resultUrl="alipays : //platformapi/startapp?appId=20000067&url="+ URLEncoder.encode(response.getBody()) +";";

            String resultUrl=response.getBody();
            Syslog.info(resultUrl);

            returnData.setData(resultUrl);

        } else {
            System.out.println("调用失败");

            returnData.setResult(false);
        }

        return returnData;
    }


  public static void main(String[] strings){



  }


}
